close all
clear
clc

addpath(genpath('.\functions\'))


% define the directory containing the scene frames
directory = 'C:\Users\Adriano\Documents\DATASET\Webcam DATASET\FulvioTesti3\';

%% Extraction of the feature vector from the training video

% training video is contained in the subfolder "No Tamper"
training_dir = [directory, '\No Tamper\'];

% defining the extension used for the frame files
FILETYPE = 'jpg';
% defining the size of each frame (mandatory in case of using yuv files)
FRAMESIZE = [768 1024];

params = defineParams(FILETYPE, FRAMESIZE);

featureVector = extractFeatureVector(training_dir, params);

% saving the feature vector matrix
if ~exist([ training_dir '/Feature Vector'], 'dir')
    mkdir([ training_dir '/Feature Vector'])
end
save([ training_dir '/Feature Vector/featureVector.mat' ], 'featureVector')
 
%% Creation of the map

map = createMap( featureVector, params );

% defining the thresholds
[ energyThresholds, lumaThresholds ] = defineThresholds( map, training_dir, params );

% saving the map
save([ training_dir '/Feature Vector/map.mat' ], 'map')

% saving the thresholds
save([ training_dir '/Feature Vector/thresholds.mat' ], 'energyThresholds', 'lumaThresholds')

%% Creation of an image in order to view the regions extracted by the map
cmap = jet(max(map(:)-min(map(:))+1)); % colormap in order to view colored regions
% RGB image derived from the colormap
red = cmap(:,1);
green = cmap(:,2);
blue = cmap(:,3);
temp=map+1-min(map(:));
fig(:,:,1)=red(temp);
fig(:,:,2)=green(temp);
fig(:,:,3)=blue(temp);
% saving the image into a png file
imwrite(fig,jet, [training_dir, '/Feature Vector/map.png']);

%% Tampering Detection
tamper_dir = [directory '\No Tamper\'];
detections = detectTamper( map, energyThresholds, lumaThresholds, tamper_dir, params );
save([ tamper_dir '\detections.mat' ], 'detections')

%% Show feature signals

num_frames = size(detections(1).energy,2);
%% Energy
% Detection on total scene
figTot = figure;
subplot(1,2,1);
plot(detections(1).energy, 'r.')
hold on
plot(1:num_frames, energyThresholds(1,1) * ones(1, num_frames))
plot(1:num_frames, energyThresholds(1,2) * ones(1, num_frames))
plot(detections(1).energyTampered, detections(1).energy(detections(1).energyTampered), 'g*')
title('Energy gradient on total scene')
legend('Energy Gradient', 'Lower Bound', 'Upper Bound', 'Tampering')
hold off
subplot(1,2,2);
plot(detections(1).energy_derivative)
title('Temporal Derivative of Energy gradient on total scene')
legend('Temporal Derivative of Energy Gradient')
savefig(figTot, [tamper_dir '\tamperDetectionEnergyTotal.fig'])

% Detection on regions
for ii = 1:max(map(:))
    figParz(ii) = figure;
    subplot(2,2,1)
    imshow(map == ii)
    subplot(2,2,2)
    plot(detections(ii + 1).energy, 'r.')
    hold on
    plot(1:num_frames, energyThresholds(ii + 1,1) * ones(1, num_frames))
    plot(1:num_frames, energyThresholds(ii + 1,2) * ones(1, num_frames))
    plot(detections(ii + 1).energyTampered, detections(ii + 1).energy(detections(ii + 1).energyTampered), 'g*')
    title(['Energy gradient on region ' num2str(ii)])
    legend('Energy Gradient', 'Lower Bound', 'Upper Bound', 'Tampering')
    hold off
    subplot(2,2,3)
    plot(detections(ii + 1).energy_derivative)
    title(['Temporal Derivative of Energy gradient on region ' num2str(ii)])
    legend('Temporal Derivative of Energy Gradient')
    savefig(figParz(ii), [tamper_dir '\tamperDetectionEnergyRegion', num2str(ii),'.fig'])
end

%% Luma
% Detection on total scene
figTot = figure;
plot(detections(1).luma, 'r.')
hold on
plot(1:num_frames, lumaThresholds(1,1) * ones(1, num_frames))
plot(1:num_frames, lumaThresholds(1,2) * ones(1, num_frames))
plot(detections(1).lumaTampered, detections(1).luma(detections(1).lumaTampered), 'g*')
title('Mean Luma on total scene')
legend('Mean Luma', 'Lower Bound', 'Upper Bound', 'Tampering')
hold off
savefig(figTot, [tamper_dir '\tamperDetectionLumaTotal.fig'])

% Detection on regions
for ii = 1:max(map(:))
    figParz(ii) = figure;
    subplot(1,2,1)
    imshow(map == ii)
    subplot(1,2,2)
    plot(detections(ii + 1).luma, 'r.')
    hold on
    plot(1:num_frames, lumaThresholds(ii + 1,1) * ones(1, num_frames))
    plot(1:num_frames, lumaThresholds(ii + 1,2) * ones(1, num_frames))
    plot(detections(ii + 1).lumaTampered, detections(ii + 1).luma(detections(ii + 1).lumaTampered), 'g*')
    title(['Mean Luma on region ' num2str(ii)])
    legend('Mean Luma', 'Lower Bound', 'Upper Bound', 'Tampering')
    hold off
    savefig(figParz(ii), [tamper_dir '\tamperDetectionLumaRegion', num2str(ii),'.fig'])
end

